<template>
  <div class="sortingParameterIndex">
      <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>分类参数</el-breadcrumb-item>
</el-breadcrumb>
<!-- Cart卡片视图 -->
<el-card class="box-card">
  <el-alert
  title="注意：只允许第三级分类设置相关参数！！！"
  type="warning"
  show-icon
  ></el-alert>
   <!-- 选择商品分类 -->
   <div class="options">
     <span>选择商品分类：</span>
    <el-cascader
    expand-trigger= 'hover'
    v-model="selectOption"
    :options="sortingData"
    :props="sortingProps"
    clearable
    @change="shopStortingChange">
    </el-cascader>
    </div>
    <el-tabs
    v-model="activeName" @tab-click="handleClick"
    >
    <el-tab-pane label="动态参数" name="many">
      <el-row>
  <el-button type="primary" :disabled="isBtnDisplayHide" @click="addArgument">动态参数</el-button>
  <el-table
    :data="fullData"
    border
    style="width: 100%">
      <el-table-column
      type="expand"
      width="50">
      <template slot-scope="scope">
        <!-- 一级 -->
          <el-tag v-for="(item,index) in scope.row.attr_vals" :key="index" closable>{{item}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column
      type="index"
      width="50">
    </el-table-column>
    <el-table-column
      prop="attr_name"
      label="参数名称"
      >
    </el-table-column>
    <el-table-column
      label="操作"
        >
        <template slot-scope="scope">
          <div class="tableBtn">
            <!-- 修改用户按钮 -->
            <el-button @click="amendUsers(scope.row)" type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            <!-- 删除用户按钮 -->
            <el-button @click="deleteParameter(scope.row)" type="danger" size="mini" icon="el-icon-delete">删除</el-button>
          </div>
        </template>
    </el-table-column>
  </el-table>
</el-row>
    </el-tab-pane>
    <el-tab-pane label="静态属性" name="only">
      <el-row>
  <el-button type="primary" :disabled="isBtnDisplayHide" @click="addArgument">静态属性</el-button>
<el-table
    :data="fullData"
    border
    style="width: 100%">
      <el-table-column
      type="expand"
      width="50">
      <template slot-scope="scope">
        <!-- 一级 -->
        <el-tag v-for="(item,index) in scope.row.attr_vals" :key="index" closable>{{item}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column
      type="index"
      width="50">
    </el-table-column>
    <el-table-column
      prop="attr_name"
      label="属性名称"
      >
    </el-table-column>
    <el-table-column
      label="操作"
        >
        <template slot-scope="scope">
          <div class="tableBtn">
            <!-- 修改参数按钮 -->
            <el-button @click="amendUsers(scope.row.id)" type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <!-- 删除参数列表按钮 -->
            <el-button @click="deleteUser(scope.row.id)" type="danger" size="mini" icon="el-icon-delete">删除</el-button>
          </div>
        </template>
    </el-table-column>
  </el-table>
</el-row>
    </el-tab-pane>
    </el-tabs>
</el-card>
 <!-- 添加动态参数、静态属性 -->
      <el-dialog
      @close="closeArguments"
  :title="'添加'+ onlyManyText"
  :visible.sync="addArgumentVisible"
  width="50%"
  >
  <div>
    <el-form :model="addAllForm"
    ref="addAllForm"
    :rules="rules"
    label-width="100px">
   <el-form-item :label="onlyManyText" prop="attr_name">
    <el-input v-model="addAllForm.attr_name"></el-input>
  </el-form-item>
</el-form>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitArguments">确 定</el-button>
    <el-button @click="addArgumentVisible = false">取 消</el-button>
    <el-button @click="resetForm('addAllForm')">重置</el-button>
  </span>
    </el-dialog>
    <!-- 修改参数 -->
       <el-dialog
      @close="closeArguments"
      :title="'修改'+ amendOnlyManyText"
      :visible.sync="amentArgumentVisible"
      width="50%"
  >
  <div>
    <el-form :model="amentAllForm"
    ref="addAllForm"
    :rules="rules"
    label-width="100px">
   <el-form-item :label="onlyManyText" prop="attr_name">
    <el-input v-model="amentAllForm.attr_name"></el-input>
  </el-form-item>
</el-form>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitamentArguments">确 定</el-button>
    <el-button @click="amendArgumentVisible = false">取 消</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {getCategories,getCategoriesId,postCategoriesId,deleteCategoriesIdAttrid,getCategoriesAttrId,putCategoriesAttrId} from '@/api/shopManage'
export default {
name:'sortingParameterIndex',
data() {
  return {
  activeName:'many',
  sortingData:[],//选择商品分类数据
  fullData:[],//动态参数、静态属性数据
  sortingProps:{
       value:'cat_id',
        label:'cat_name',
        children:'children'
  },
  selectOption:[],//级联双向绑定
  addArgumentVisible:false,//添加动态参数、静态属性
  amentArgumentVisible:false,//修改动态参数、静态属性
   rules:{
            attr_name: [
            { required: true, message: '请输入参数名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
        },
         addAllForm:{
           attr_name:''
        },
        // 修改参数
        amentAllForm:{}
  }
},
created(){
   this.sortingParameter()
},
computed: {
  // 计算select是否存在选择值
  isBtnDisplayHide(){
    if(this.selectOption.length !==3){
      return true
    }
    return false
  },
  //使用computed存放参数列表的动态参数和静态属性
  onlyMany(){
    if(this.selectOption.length === 3){
      return this.selectOption[2]
    }
    return null
  },
  // 动态计算参数数据
  onlyManyText(){
    if(this.activeName =='many'){
      return '动态参数'
    }
    return '静态属性'
  },
  // 修改动态、静态参数
  amendOnlyManyText(){
    if(this.activeName =='many'){
      return '动态参数'
    }
    return '静态属性'
  }
},
methods:{
  // 获取select商品数据
  async sortingParameter(){
   let {data} = await getCategories()
   this.sortingData=data.data
  },
  // 选择级联属性
  async shopStortingChange(){
    this.FullParameter()
  },
  // 动态参数和静态属性数据
 async FullParameter(){
     let {data} = await getCategoriesId(this.onlyMany,{
       sel:this.activeName
     })
     data.data.forEach(item => {
        item.attr_vals = item.attr_vals?item.attr_vals.split(' '):[]
     });
     console.log(data.data, '')
     console.log(data, '动态参数和静态属性数据')
     if(data.meta.status!==200) return this.$message.error(data.meta.msg)
     this.fullData = data.data;
},
  // 切换动态参数、静态属性
  handleClick(){
    // console.log(this.activeName, '')
    this.FullParameter()
  },
  // 添加动态参书、静态属性
  addArgument(){
      this.addArgumentVisible=true
      console.log(this.activeName, '')
  },
  // 添加属性提交
 async submitArguments(){
    let {data} = await postCategoriesId(this.onlyMany,{
      attr_name:this.addAllForm.attr_name,
      attr_sel:this.activeName,
    })
    console.log(data, '')
    if(data.meta.status !==201) return this.$message.error(data.meta.msg)
    this.$message.success(data.meta.msg)
    this.FullParameter()
    this.addArgumentVisible=false
  },
  // 重置
  resetForm(){
    this.$refs.addAllForm.resetFields()
  },
  // close开启重置
  closeArguments(){
    this.$refs.addAllForm.resetFields()
  },
  // 删除参数
  async deleteParameter(paramsID){
      const res = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err);
        if(res !== 'confirm')return this.$message.info('取消了删除')
        let Id = paramsID.cat_id;
     let attrId = paramsID.attr_id;
      let {data} = await deleteCategoriesIdAttrid(Id,attrId)
      console.log(data, '')
      if(data.meta.status !==200) return this.$message.error(data.meta.msg)
      this.$message.success(data.meta.msg)
      this.FullParameter()
  },
  // 修改动态参数、静态属性
  async amendUsers(paramsID){
    let Id = paramsID.cat_id;
     let attrId = paramsID.attr_id;
    this.amentArgumentVisible=true
    let {data} = await getCategoriesAttrId(Id,attrId,this.activeName)
    this.amentAllForm = data.data
  },
  // 编辑提交参数
 async submitamentArguments(){
    let Id = this.amentAllForm.cat_id;
     let attrId = this.amentAllForm.attr_id;
    let {data} = await putCategoriesAttrId(this.activeName,Id,attrId,this.amentAllForm)
    if(data.meta.status !== 200) return this.$message.error(data.meta.msg)
    this.amentArgumentVisible = false
    this.FullParameter()
    this.$message.success(data.meta.msg)
  }
}
}
</script>

<style lang="less" scoped>
.sortingParameterIndex{
  .options{
    margin: 20px 0;
  }
 /deep/ .el-table .cell{
    display: flex;
    flex-direction: row;
  }
  .el-tag {
    margin:0 10px
  }
}
</style>
